/* utime.c
 *
 * utime - change file last access and modification times 
 * Copyright (C) 2011 Ivan Totev <ivan.totev.85@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <getopt.h>
#include <sys/stat.h>
#include <utime.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#define PROGRAM "utime"
#define VERSION "0.1"

void print_help(int exit_val) {
	printf("\nChange file last access and modification times\n\n");
	printf("%s [-m timestamp or string] [-a timestamp or string] filename1 filename2 ...\n", PROGRAM);
	printf("  -h		print this help and exit\n");
	printf("  -V		print version and exit\n");
	printf("  -v		verbose\n");
	printf("  -m		set last modification time (timestamp or time string in format %%Y-%%m-%%d %%H:%%M:%%S)\n");
	printf("  -a		set last access time (timestamp or time string in format %%Y-%%m-%%d %%H:%%M:%%S)\n");
	exit(exit_val);
}

int main(int argc, char **argv) {

	int opt;
	int verbose = 0;
	struct tm tm;
	struct utimbuf opt_times, times;

	memset(&tm, 0, sizeof(struct tm));

	opt_times.modtime = 0;
	opt_times.actime = 0;

	struct stat fs;
	while((opt = getopt(argc, argv, "hvVm:a:")) != -1) {
		switch(opt) {
			case 'h':
				print_help(0);
			break;
			case 'V':
				printf("%s v%s\n", PROGRAM, VERSION); 
				exit(0);
			break;
			case 'v':
				verbose=1;
			break;
			case 'm':
				if ( strstr(optarg, "-")  ) {
					if ( ! strptime(optarg, "%Y-%m-%d %H:%M:%S", &tm) ) {
						print_help(EXIT_FAILURE);
					}
					opt_times.modtime = mktime(&tm);
					memset(&tm, 0, sizeof(struct tm));

				} else {
					opt_times.modtime = atoi(optarg);
				}
			break;
			case 'a':
				if ( strstr(optarg, "-")  ) {
					if ( ! strptime(optarg, "%Y-%m-%d %H:%M:%S", &tm) ) {
						print_help(EXIT_FAILURE);
					}
					opt_times.actime = mktime(&tm);
					memset(&tm, 0, sizeof(struct tm));

				} else {
					opt_times.actime = atoi(optarg);
				}
			break;
			case ':':
				fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PROGRAM, optopt);
			break;
		}
	}

	if ( opt_times.modtime <= 0 && opt_times.actime <= 0 ) print_help(EXIT_FAILURE);
	if ( argc == optind ) print_help(EXIT_FAILURE);

	int index;
	for (index = optind; index < argc; index++) {

		if ( stat(argv[index], &fs) == -1 ) {
			printf("%s: cannot access '%s': %s \n",PROGRAM, argv[index],strerror(errno));
			continue;
		}

		if ( opt_times.modtime ) {
			times.modtime = opt_times.modtime;
			if ( verbose ) printf("Set modtime to %d\n", opt_times.modtime);
		} else {
			times.modtime = fs.st_mtime;
		}

		if ( opt_times.actime ) {
			times.actime = opt_times.actime;
			if ( verbose ) printf("Set actime to %d\n", opt_times.actime);
		} else {
			times.actime = fs.st_atime;
		}

		if ( utime(argv[index], &times) == -1 ) {
			printf("%s: cannot change time '%s': %s \n",PROGRAM, argv[index],strerror(errno));
		}
	}
	exit(EXIT_SUCCESS);
}
